package cn.joeg.simple;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;


/**
 * @author Joeg
 * @email 840459673@qq.com
 * @date 2019/7/17
 */
public class MyProducer {

    private final static String EXCHANGE_NAME = "SIMPLE_EXCHANGE";

    public static void main(String[] args) throws IOException, TimeoutException {

        ConnectionFactory factory = new ConnectionFactory();
        // 连接IP
        factory.setHost("192.168.35.136");
        // 默认监听端口
        factory.setPort(5672);
        // 虚拟机
        factory.setVirtualHost("my_vhost");

        // 设置访问的用户
        factory.setUsername("admin");
        factory.setPassword("admin");

        // 建立连接
        Connection conn = factory.newConnection();
        // 创建消息通道
        Channel channel = conn.createChannel();

        // 发送消息
        String msg = "Hello world, Rabbit MQ";


/*        // 对每条消息设置过期时间
        AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder()
                .deliveryMode(2) // 持久化消息
                .contentEncoding("UTF-8")
//                .expiration("30000") // TTL 30s
                .build();*/

        // 对每条消息设置过期时间
        AMQP.BasicProperties p1 = new AMQP.BasicProperties.Builder()
                .priority(5)
                .contentEncoding("UTF-8")
                .build();

        // 对每条消息设置过期时间
        AMQP.BasicProperties p2 = new AMQP.BasicProperties.Builder()
                .priority(10)
                .contentEncoding("UTF-8")
                .build();


        // String exchange, String routingKey, BasicProperties props, byte[] body
        channel.basicPublish("TOPIC_EXCHANGE", "hello.gupao.world", p1, (msg).getBytes());
        channel.basicPublish("TOPIC_EXCHANGE", "noob.gupao.joeg", p2, (msg).getBytes());


        channel.close();
        conn.close();


    }
}
